/**
 *  var urls = [
            //Index
            {id: "bg", src: WebPath + "image/indexImg/bg.jpg"}, 
            {id: "smokeSeq", src: WebPath + "image/seqImg/smokeSeq.png"}
        ];
        queueIndex = new MC.createjsd.LoadQueue(false);
      //  queueIndex = new createjs.LoadQueue(false);
        queueIndex.on("fileload", handleFileLoad);
        queueIndex.on("progress", handleFileProgress);
        queueIndex.on("complete", handleFileComplete);

        queueIndex.loadManifest(urls);
        createjs.Touch.enable(stage);
        queueIndex.loadBlobFile(WEB+'hello.png');
 * 
 */
module MC.createjsd {
	/** 文件列表 */
	interface FILELIST {
		id: string;///文件的id
		src: string;///文件的路径
		////	blob:Blob;///二进制引用
		url: string;///文件地址
		begin: number;///文件开始的位置
		size: number;///文件的大小
		mime: string;///文件类型
	}
	export class LoadQueue {
		public constructor(pa: boolean = false) {
			/*
			this._zip=new Zip.Zblob;
			this._zip.isFenge=true;
			let mthis=this;
			this._zip.OnError=()=>{
				mthis.OnError();
			};*/
		}
		/** 标记是否加载完成 */
		public isComplete = false;
		/** 加载完成以后调用 */
		public OnComplete() {
			let isComplete = true;
			for (let one of this._zipArray) {
				if (!one.isComplete) {
					isComplete = false;
				}
			}
			if (isComplete) {
				if (typeof (this._funList['complete']) == 'function') {
					let fun = this._funList['complete'];
					this.isComplete = true;
					///	setTimeout(function() {
					fun();
					///}, 1000*2);
					clearInterval(this._timeProgress);
				}
			}
		};
		private _zipArray: Array<Zip.Zblob> = [];
		private _filelist: Array<FILELIST> = null;
		private _filename: string = '';

		private m_allImgList: Array<any> = [];
		private m_allList: Array<any> = [];
		/** 返回加载的文件列表 */
		public getList() {
			return this.m_allList;
		}
		/** 加载文件列表 */
		public loadManifest(urls: Array<FILELIST>) {
			this._filelist = urls;
		}
		/** 注册事件 fileload 正在加载的资源		progress 加载进度		complete 加载完成时调用 */
		public on(evename: string, fun: Function) {
			this._funList[evename] = fun;
		}
		/** 事件管理 */
		private _funList: any = [];
		/** 返回指定对象的URL */
		public getResultUrl(id: string) {
			if (this.m_allImgList[<any>id]) {
				return this.m_allImgList[<any>id].url;
			} else {
				return false;
			}
		}
		/** 返回指定对象的IMG */
		public getResultImg(id: string) {
			////	return this._zip.getImg(id);
		}
		/** 返回指定对象的IMG */
		public getResult(id: string) {
			if(!this.m_allImgList[<any>id]){
				console.log("找到不到文件:"+id);
				return false;
			}
			return this.m_allImgList[<any>id].img;
		}


		/** 返回指定对象的IMG */
		public getResultBuffer(id: string) {
				 return this.m_allImgList[<any>id].buffer;
		}

		public load() { };
		/** 加载压缩文件 */
		public loadBlobFile(file: string) {
			let mthis = this;
			let _zip = new Zip.Zblob;
			_zip.isFenge = true;
			_zip.OnError = () => {
				mthis.OnError();
			};
			this._zipArray.push(_zip);
			_zip.OntimeProgress=()=>{
				mthis.upProgress();
			}
			/*
			if (this._timeProgress == 0) {
				this._timeProgress = setInterval(() => {
					mthis.upProgress();
				}, 300);
			}*/
			this._filename = file;
			//	this._zip.openFileList();
			_zip.Onload = (url: string) => {
				_zip.ReadImgs(() => {
					///	if(typeof(mthis._funList['complete'])=='function'){
					let list = _zip.getList();
					for (let one of list) {
						mthis.m_allImgList[one.id] = _zip.getNodeObj(one.id);
						mthis.m_allList.push(_zip.getNodeObj(one.id));
					}
					/// console.log(mthis.m_allImgList);
					mthis.OnComplete();
					///	}
					/*	mthis.OnProgress({
							progress: 1
						});*/
				});

			};
			_zip.open(file);//filelist 可以为空,
		}

		/**
		 * 定时器，刷新进度
		 */
		private upProgress() {
			let pro = 0;
			for (let one of this._zipArray) {
				pro += one.progress;
			}
			/*
			 console.log(this._zipArray[0].progress+"\t"
			+this._zipArray[1].progress+"\t"
			+this._zipArray[2].progress+"\t"+pro+"\t");
			 */
			this.OnProgress({
				progress: (pro / this._zipArray.length / 100)
			});
		}
		private _timeProgress = 0;


		/** 加载错误的时候调用 */
		private OnError() {
			console.log("加载错误");
			if (debug.isdebug) {//如果处于调试状态,则显示命令行
				this.showLine();
			} else {
				setTimeout(function () {
					//	location.reload();
				}, 1000);
			}
		}
		private showLine() {
			//	console.log(this._filelist);
			let line = '';/*
			for(let one of this._filelist){
				line+=
			}*/
			line = JSON.stringify(this._filelist);
			//'java -jar run2.jar ' + 
			///	console.log("请在命令行执行:%c"+line, "color:white;    background-color: black;");
			line = encodeURIComponent(line) + ' ' + this._filename +' 3';
			console.log("请在命令行执行:",line);///, "color:white;    background-color: black;"
		}
		/** progress */
		public OnProgress(event: any) {
			if (typeof (this._funList['progress']) == 'function') {
				this._funList['progress'](event);
			}
			//p = Math.floor(event.progress * 100);
		}

		public clear() {
			if (!this.m_allList) {
				return;
			}
			for (var i = 0; i < this.m_allList.length; i++) {
				URL.revokeObjectURL(this.m_allList[i].url);
				this.m_allList[i].img = null;
				this.m_allList[i].blob = null;
				this.m_allList[i].buffer = null;
				this.m_allList[i] = null;
			}
			this.m_allImgList = null;
			this.m_allList = null;
		}

	}
}